package com.oig.sys.base.app.spider;

import lombok.extern.slf4j.Slf4j;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

@Slf4j
public class CurrRateBocPageProcessor implements PageProcessor {

    private final static Site site = Site.me()
            .setRetryTimes(3)
            .setRetrySleepTime(3000)
            .setSleepTime(5*1000)
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3")
            .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
            .setCharset("UTF-8")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36");


    @Override
    public void process(Page page) {
        List<String> currNameList = page.getHtml().xpath("//div[@class='BOC_main']/div[1]/div[2]/table/tbody/tr/td[1]/text()").all();
        List<String> bankBuyRateList = page.getHtml().xpath("//div[@class='BOC_main']/div[1]/div[2]/table/tbody/tr/td[2]/text()").all();    //现汇买入价-银行买入价？
        List<String> currRateList = page.getHtml().xpath("//div[@class='BOC_main']/div[1]/div[2]/table/tbody/tr/td[4]/text()").all();       //现汇卖出价-银行卖出价格？就是我们的购汇汇率
        List<String> customsRateList = page.getHtml().xpath("//div[@class='BOC_main']/div[1]/div[2]/table/tbody/tr/td[6]/text()").all();      // 中行折算价（海关汇率）
        List<String> currDateList = page.getHtml().xpath("//div[@class='BOC_main']/div[1]/div[2]/table/tbody/tr/td[7]/text()").all();
        List<String> currTimeList = page.getHtml().xpath("//div[@class='BOC_main']/div[1]/div[2]/table/tbody/tr/td[8]/text()").all();
//        List<String> textList = page.getHtml().xpath("//div[@class='BOC_main']/div[1]/div[2]/table/tbody/tr/td/text()").all();
        page.putField("currNameList", currNameList);
        page.putField("bankBuyRateList", bankBuyRateList);
        page.putField("currRateList", currRateList);
        page.putField("customsRateList", customsRateList);
        page.putField("currDateList", currDateList);
        page.putField("currTimeList", currTimeList);
        log.debug("process page:currName-{}-", currNameList);
    }

    @Override
    public Site getSite() {
        return site;
    }
}
